package zero.springdemo.springquartzdemo.core.config

import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.scheduling.quartz.CronTriggerFactoryBean
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean


@Configuration
open class QuartzJobConfig {
    /**
     * 方法调用任务明细工厂Bean
     */
    @Bean(name = ["myFirstExerciseJobBean"])
    open fun myFirstExerciseJobBean(myFirstExerciseJob: MyFirstExerciseJob): MethodInvokingJobDetailFactoryBean {
        val jobDetail = MethodInvokingJobDetailFactoryBean()
        jobDetail.setConcurrent(false) // 是否并发
        jobDetail.setName("general-myFirstExerciseJob") // 任务的名字
        jobDetail.setGroup("general") // 任务的分组
        jobDetail.targetObject = myFirstExerciseJob // 被执行的对象
        jobDetail.targetMethod = "myJobBusinessMethod" // 被执行的方法
        return jobDetail
    }

    /**
     * 表达式触发器工厂Bean
     */
    @Bean(name = ["myFirstExerciseJobTrigger"])
    open fun myFirstExerciseJobTrigger(@Qualifier("myFirstExerciseJobBean") myFirstExerciseJobBean: MethodInvokingJobDetailFactoryBean): CronTriggerFactoryBean {
        val tigger = CronTriggerFactoryBean()
        tigger.setJobDetail(myFirstExerciseJobBean.getObject())
        tigger.setCronExpression("0/10 * * * * ?") // 什么是否触发，Spring Scheduler Cron表达式
        tigger.setName("general-myFirstExerciseJobTrigger")
        return tigger
    }



    /**
     * 表达式触发器工厂Bean
     */
    @Bean(name = ["mySecondExerciseJobTrigger"])
    open fun mySecondExerciseJobTrigger(@Qualifier("mySecondExerciseJobBean") mySecondExerciseJobDetailFactoryBean: MethodInvokingJobDetailFactoryBean): CronTriggerFactoryBean {
        val tigger = CronTriggerFactoryBean()
        tigger.setJobDetail(mySecondExerciseJobDetailFactoryBean.getObject())
        tigger.setCronExpression("0/10 * * * * ?") // 什么是否触发，Spring Scheduler Cron表达式
        tigger.setName("general-mySecondExerciseJobTrigger")
        return tigger
    }


    /**
     * 方法调用任务明细工厂Bean
     */
    @Bean(name = ["mySecondExerciseJobBean"])
    open fun mySecondExerciseJobBean(mySecondExerciseJob: MySecondExerciseJob): MethodInvokingJobDetailFactoryBean {
        val jobDetail = MethodInvokingJobDetailFactoryBean()
        jobDetail.setConcurrent(false) // 是否并发
        jobDetail.setName("general-mySecondExerciseJob") // 任务的名字
        jobDetail.setGroup("general") // 任务的分组
        jobDetail.targetObject = mySecondExerciseJob // 被执行的对象
        jobDetail.targetMethod = "myJobBusinessMethod" // 被执行的方法
        return jobDetail
    }

}